package com.waterworld.watch.chat.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.waterworld.watch.R;
import com.waterworld.watch.chat.adapter.ChatAdapter;
import com.waterworld.watch.chat.interfaces.IChatManager;
import com.waterworld.watch.chat.manager.ChatManager;
import com.waterworld.watch.common.bean.ChatGroupBean;
import com.waterworld.watch.common.customview.MyPtrHeaderView;
import com.waterworld.watch.common.net.BaseObserverListener;
import com.waterworld.watch.common.net.BaseResultBean;
import com.waterworld.watch.home.activity.HomePagerActivity;
import com.waterworld.watch.login.event.AutoLoginEvent;
import com.waterworld.watch.push.contant.MessageContants;
import com.waterworld.watch.push.event.MessageEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;


public class ChatFragment extends Fragment {

    @BindView(R.id.rv_chat)
    RecyclerView rv_chat;

    @BindView(R.id.ptr_frame)
    PtrFrameLayout ptr_frame;

    private HomePagerActivity parent;

    private IChatManager iChatManager = ChatManager.getInstance();
    private List<ChatGroupBean> mList = new ArrayList<>();
    private ChatAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_chat,container,false);
        ButterKnife.bind(this,view);
        parent = (HomePagerActivity) getActivity();
        EventBus.getDefault().register(this);
        initView();
        return view;
    }

    private void initView() {
        MyPtrHeaderView header = new MyPtrHeaderView(parent);
        ptr_frame.setKeepHeaderWhenRefresh(true);
        ptr_frame.setHeaderView(header);
        ptr_frame.addPtrUIHandler(header);
        ptr_frame.disableWhenHorizontalMove(true);
        PtrHandler handler = new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                ptr_frame.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        initData();
                    }
                }, 500);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                //解决RecycleView和PtrFrameLayout滑动冲突问题
                return !rv_chat.canScrollVertically(-1);
            }
        };
        ptr_frame.setPtrHandler(handler);
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        if (iChatManager != null) {
            iChatManager.listGroup(new BaseObserverListener<BaseResultBean<ChatGroupBean[]>>() {
                @Override
                public void onCompleted() {
                    ptr_frame.refreshComplete();
                }

                @Override
                public void onError(Throwable e) {
                }

                @Override
                public void onNext(BaseResultBean<ChatGroupBean[]> baseResultBean) {
                    if (baseResultBean.getData() != null) {
                        mList.clear();
                        mList.addAll(Arrays.asList(baseResultBean.getData()));
                        mAdapter = new ChatAdapter(parent,mList);
                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(parent);
                        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                        rv_chat.setLayoutManager(linearLayoutManager);
                        rv_chat.setAdapter(mAdapter);
                    }
                }
            });
        }
    }

    private static final int HAS_NEW_MESSAGE = 0x01;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void hasNewMsg(MessageEvent event) {
        if (event.getType() == MessageContants.CHAT) {
            for (int i=0;i<mList.size();i++) {
                if (mList.get(i).getGroupId() == event.getGroupId()) {
                    initData();
                }
            }
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
